package com.xzq.erp.controller;


import com.xzq.erp.aspect.ApiOperationLog;
import com.xzq.erp.domain.dto.CustomerDTO;
import com.xzq.erp.domain.dto.PageDTO;
import com.xzq.erp.domain.query.CustomerPageQuery;
import com.xzq.erp.domain.vo.CustomerVO;
import com.xzq.erp.service.ICustomerService;
import com.xzq.erp.utils.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 客户信息表 前端控制器
 * </p>
 *
 * @author xzq
 * @since 2025-05-13
 */
@RestController
@RequestMapping("/customer")
@Tag(name = "客户信息相关接口")
@RequiredArgsConstructor
@Validated
public class CustomerController {

    private final ICustomerService customerService;

    /**
     * 分页查询客户列表
     */
    @GetMapping("/list")
    @Operation(summary = "分页获取客户列表")
    @ApiOperationLog(description = "分页获取客户列表")
    @PreAuthorize("hasAuthority('customer:list') || hasRole('ADMIN')")
    public Response<PageDTO<CustomerVO>> getCustomerList(@ParameterObject @Validated CustomerPageQuery query) {
        return Response.success(customerService.getCustomerList(query));
    }

    /**
     * 添加客户
     */
    @PostMapping
    @Operation(summary = "添加客户")
    @ApiOperationLog(description = "添加客户")
    @PreAuthorize("hasAuthority('customer:add') || hasRole('ADMIN')")
    public Response addCustomer(@RequestBody @Validated CustomerDTO customerDTO) {
        customerService.addCustomer(customerDTO);
        return Response.success();
    }

    /**
     * 修改客户
     */
    @PutMapping
    @Operation(summary = "修改客户")
    @ApiOperationLog(description = "修改客户")
    @PreAuthorize("hasAuthority('customer:update') || hasRole('ADMIN')")
    public Response updateCustomer(@RequestBody @Validated CustomerDTO customerDTO) {
        customerService.updateCustomer(customerDTO);
        return Response.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping
    @Operation(summary = "批量删除客户")
    @ApiOperationLog(description = "批量删除客户")
    @PreAuthorize("hasAuthority('customer:delete') || hasRole('ADMIN')")
    public Response deleteCustomer(@RequestBody @NotEmpty List<Long> ids) {
        customerService.removeByIds(ids);
        return Response.success();
    }
}
